package com.turing.usdtpay.mvp.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.turing.usdtpay.R;
import com.turing.usdtpay.base.BaseFragment;
import com.turing.usdtpay.entity.OrderAllData;
import com.turing.usdtpay.entity.OrderInfoData;
import com.turing.usdtpay.entity.OrderManagerData;
import com.turing.usdtpay.mvp.contract.OrderManagerContract;
import com.turing.usdtpay.mvp.presenter.OrderManagerPresenter;
import com.turing.usdtpay.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

import com.turing.usdtpay.adapter.PagerAdapter;
import com.turing.usdtpay.utils.ToastUtils;

/**
 * @作者：JAY
 * @创建时间：2019/10/12 16:16
 * @描述：
 **/
public class OrderManagerFragment extends BaseFragment implements OrderManagerContract.View{
    private String TAG = "OrderManagerFragment";
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private ArrayList<String> mTitle = new ArrayList<>();
    private ArrayList<String> mCount = new ArrayList<>();
    private String[] titles = {"进行中", "已完成", "已取消"};
    private String[] count = {"0", "0", "0"};
    private List<OrderListFragment> mFragmentList = new ArrayList<>();
    private OrderManagerContract.Present mPresent;
    private PagerAdapter mAdapter;
    public static OrderManagerFragment newInstance(Bundle bundle) {
        OrderManagerFragment fragment = new OrderManagerFragment();
        if (bundle != null) {
            fragment.setArguments(bundle);
        }
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setPresenter(new OrderManagerPresenter(this));
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order_tab, container, false);
        initView(view);
        initTab();
        initFragment();
        initViewPager();
        showProgressBar(true);
        initData();
        return view;
    }

    public void initData() {
        if (mPresent != null){
            mPresent.getOrderList(getActivity(),1,1,10,true);
        }

    }

    private void initViewPager() {
        mAdapter = new PagerAdapter(getActivity().getSupportFragmentManager(), mFragmentList, mTitle, mCount);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setOffscreenPageLimit(3);
        //为ViewPager添加页面改变监听
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));

    }

    private void initTab() {
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTitle = new ArrayList<>();
        for (int i = 0; i < titles.length; i++) {
            mTitle.add(titles[i]);
            String title = titles[i] + "<font color='#ff0000'>(" + count[i] + ")</font>";
            mTabLayout.addTab(mTabLayout.newTab().setText(Html.fromHtml(title)));
            mCount.add(count[i]);
        }
//        //为TabLayout添加Tab选择监听
//        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                LogUtils.d(TAG + ",setCurrentItem -->" + tab.getPosition());
//                mViewPager.setCurrentItem(tab.getPosition());
//            }
//
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//
//            }
//
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//
//            }
//        });
                //将tablayout与fragment关联
        mTabLayout.setupWithViewPager(mViewPager);
    }

    private void initFragment() {
        mFragmentList.clear();
        Bundle bundle = new Bundle();
        bundle.putInt("type", OrderListFragment.TYPE_GOING);
        OrderListFragment fragment1 = OrderListFragment.newInstance(bundle);
//        fragment1.setFreshListener(this);
        mFragmentList.add(fragment1);

        Bundle bundle2 = new Bundle();
        bundle2.putInt("type", OrderListFragment.TYPE_COMPLETE);
        OrderListFragment fragment2 = OrderListFragment.newInstance(bundle2);
//        fragment2.setFreshListener(this);
        mFragmentList.add(fragment2);

        Bundle bundle3 = new Bundle();
        bundle3.putInt("type", OrderListFragment.TYPE_CANCEL);
        OrderListFragment fragment3 = OrderListFragment.newInstance(bundle3);
//        fragment3.setFreshListener(this);
        mFragmentList.add(fragment3);
        LogUtils.d(TAG + ",mFragmentList -->" + mFragmentList.size());
    }

    private void initView(View view) {
        setTitle("订单管理");
        mTabLayout = (TabLayout) view.findViewById(R.id.order_tab);
        mViewPager = (ViewPager) view.findViewById(R.id.order_vp);
    }

//    @Override
//    public void onFresh(int type, int num) {
//        if (type == 1){
//            count[0] = num+"";
//        }else if (type == 2){
//            count[1] = num+"";
//        }else if (type == 3){
//            count[2] = num+"";
//        }
//        updateTab();
//    }

    private void updateTab(){
        mTitle.clear();
        mCount.clear();
        mTabLayout.removeAllTabs();
        for (int i = 0; i < titles.length; i++) {
            mTitle.add(titles[i]);
            String title = titles[i] + "<font color='#ff0000'>(" + count[i] + ")</font>";
            mTabLayout.addTab(mTabLayout.newTab().setText(Html.fromHtml(title)));
            mCount.add(count[i]);
        }
        mAdapter.updateTitle(mTitle,mCount);

    }

    @Override
    public void onSuccess(OrderManagerData data, int type, boolean fresh) {
        showProgressBar(false);
        LogUtils.d(hashCode()+TAG+" data = "+data);
        List<OrderAllData> all_order = data.getAll_order_num();
        if (all_order.size()>0){
            for (OrderAllData allData : all_order) {
                if (allData.getStatus() == 1) {
                    count[0] = allData.getNum()+"";
                }
                if (allData.getStatus() == 2) {
                    count[1] = allData.getNum()+"";
                }
                if (allData.getStatus() == 3) {
                    count[2] = allData.getNum()+"";
                }
            }
            updateTab();
        }
    }

    @Override
    public void cancelSuccess() {

    }

    @Override
    public void getSuccess() {

    }

    @Override
    public void paySuccess() {

    }

    @Override
    public void getOrderSuccess(OrderInfoData data, int type) {

    }

    @Override
    public void onFail(String msg) {
        ToastUtils.showNormolToast(getActivity(),msg);
    }

    @Override
    public void setPresenter(OrderManagerContract.Present presenter) {
        mPresent = presenter;
    }
}
